<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>iframeloadmonitor.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_iframeloadmonitor.js.html">iframeloadmonitor.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2008 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Class that can be used to determine when an iframe is loaded.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.net.IframeLoadMonitor&#39;);
<a name="line20"></a>
<a name="line21"></a>goog.require(&#39;goog.dom&#39;);
<a name="line22"></a>goog.require(&#39;goog.events&#39;);
<a name="line23"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line24"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line25"></a>goog.require(&#39;goog.userAgent&#39;);
<a name="line26"></a>
<a name="line27"></a>
<a name="line28"></a>
<a name="line29"></a>/**
<a name="line30"></a> * The correct way to determine whether an iframe has completed loading
<a name="line31"></a> * is different in IE and Firefox.  This class abstracts above these
<a name="line32"></a> * differences, providing a consistent interface for:
<a name="line33"></a> * &lt;ol&gt;
<a name="line34"></a> * &lt;li&gt; Determing if an iframe is currently loaded
<a name="line35"></a> * &lt;li&gt; Listening for an iframe that is not currently loaded, to finish loading
<a name="line36"></a> * &lt;/ol&gt;
<a name="line37"></a> *
<a name="line38"></a> * @param {HTMLIFrameElement} iframe An iframe.
<a name="line39"></a> * @param {boolean=} opt_hasContent Does the loaded iframe have content.
<a name="line40"></a> * @extends {goog.events.EventTarget}
<a name="line41"></a> * @constructor
<a name="line42"></a> */
<a name="line43"></a>goog.net.IframeLoadMonitor = function(iframe, opt_hasContent) {
<a name="line44"></a>  goog.base(this);
<a name="line45"></a>
<a name="line46"></a>  /**
<a name="line47"></a>   * Iframe whose load state is monitored by this IframeLoadMonitor
<a name="line48"></a>   * @type {HTMLIFrameElement}
<a name="line49"></a>   * @private
<a name="line50"></a>   */
<a name="line51"></a>  this.iframe_ = iframe;
<a name="line52"></a>
<a name="line53"></a>  /**
<a name="line54"></a>   * Whether or not the loaded iframe has any content.
<a name="line55"></a>   * @type {boolean}
<a name="line56"></a>   * @private
<a name="line57"></a>   */
<a name="line58"></a>  this.hasContent_ = !!opt_hasContent;
<a name="line59"></a>
<a name="line60"></a>  /**
<a name="line61"></a>   * Whether or not the iframe is loaded.
<a name="line62"></a>   * @type {boolean}
<a name="line63"></a>   * @private
<a name="line64"></a>   */
<a name="line65"></a>  this.isLoaded_ = this.isLoadedHelper_();
<a name="line66"></a>
<a name="line67"></a>  if (!this.isLoaded_) {
<a name="line68"></a>    // IE 6 (and lower?) does not reliably fire load events, so listen to
<a name="line69"></a>    // readystatechange.
<a name="line70"></a>    // IE 7 does not reliably fire readystatechange events but listening on load
<a name="line71"></a>    // seems to work just fine.
<a name="line72"></a>    var isIe6OrLess = goog.userAgent.IE &amp;&amp; !goog.userAgent.isVersion(&#39;7&#39;);
<a name="line73"></a>    var loadEvtType = isIe6OrLess ?
<a name="line74"></a>        goog.events.EventType.READYSTATECHANGE : goog.events.EventType.LOAD;
<a name="line75"></a>    this.onloadListenerKey_ = goog.events.listen(
<a name="line76"></a>        this.iframe_, loadEvtType, this.handleLoad_, false, this);
<a name="line77"></a>
<a name="line78"></a>    // Sometimes we still don&#39;t get the event callback, so we&#39;ll poll just to
<a name="line79"></a>    // be safe.
<a name="line80"></a>    this.intervalId_ = window.setInterval(
<a name="line81"></a>        goog.bind(this.handleLoad_, this),
<a name="line82"></a>        goog.net.IframeLoadMonitor.POLL_INTERVAL_MS_);
<a name="line83"></a>  }
<a name="line84"></a>};
<a name="line85"></a>goog.inherits(goog.net.IframeLoadMonitor, goog.events.EventTarget);
<a name="line86"></a>
<a name="line87"></a>
<a name="line88"></a>/**
<a name="line89"></a> * Event type dispatched by a goog.net.IframeLoadMonitor when it internal iframe
<a name="line90"></a> * finishes loading for the first time after construction of the
<a name="line91"></a> * goog.net.IframeLoadMonitor
<a name="line92"></a> * @type {string}
<a name="line93"></a> */
<a name="line94"></a>goog.net.IframeLoadMonitor.LOAD_EVENT = &#39;ifload&#39;;
<a name="line95"></a>
<a name="line96"></a>
<a name="line97"></a>/**
<a name="line98"></a> * Poll interval for polling iframe load states in milliseconds.
<a name="line99"></a> * @type {number}
<a name="line100"></a> * @private
<a name="line101"></a> */
<a name="line102"></a>goog.net.IframeLoadMonitor.POLL_INTERVAL_MS_ = 100;
<a name="line103"></a>
<a name="line104"></a>
<a name="line105"></a>/**
<a name="line106"></a> * Key for iframe load listener, or null if not currently listening on the
<a name="line107"></a> * iframe for a load event.
<a name="line108"></a> * @type {goog.events.Key}
<a name="line109"></a> * @private
<a name="line110"></a> */
<a name="line111"></a>goog.net.IframeLoadMonitor.prototype.onloadListenerKey_ = null;
<a name="line112"></a>
<a name="line113"></a>
<a name="line114"></a>/**
<a name="line115"></a> * Returns whether or not the iframe is loaded.
<a name="line116"></a> * @return {boolean} whether or not the iframe is loaded.
<a name="line117"></a> */
<a name="line118"></a>goog.net.IframeLoadMonitor.prototype.isLoaded = function() {
<a name="line119"></a>  return this.isLoaded_;
<a name="line120"></a>};
<a name="line121"></a>
<a name="line122"></a>
<a name="line123"></a>/**
<a name="line124"></a> * Stops the poll timer if this IframeLoadMonitor is currently polling.
<a name="line125"></a> * @private
<a name="line126"></a> */
<a name="line127"></a>goog.net.IframeLoadMonitor.prototype.maybeStopTimer_ = function() {
<a name="line128"></a>  if (this.intervalId_) {
<a name="line129"></a>    window.clearInterval(this.intervalId_);
<a name="line130"></a>    this.intervalId_ = null;
<a name="line131"></a>  }
<a name="line132"></a>};
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * Returns the iframe whose load state this IframeLoader monitors.
<a name="line137"></a> * @return {HTMLIFrameElement} the iframe whose load state this IframeLoader
<a name="line138"></a> *     monitors.
<a name="line139"></a> */
<a name="line140"></a>goog.net.IframeLoadMonitor.prototype.getIframe = function() {
<a name="line141"></a>  return this.iframe_;
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/** @override */
<a name="line146"></a>goog.net.IframeLoadMonitor.prototype.disposeInternal = function() {
<a name="line147"></a>  delete this.iframe_;
<a name="line148"></a>  this.maybeStopTimer_();
<a name="line149"></a>  goog.events.unlistenByKey(this.onloadListenerKey_);
<a name="line150"></a>  goog.net.IframeLoadMonitor.superClass_.disposeInternal.call(this);
<a name="line151"></a>};
<a name="line152"></a>
<a name="line153"></a>
<a name="line154"></a>/**
<a name="line155"></a> * Returns whether or not the iframe is loaded.  Determines this by inspecting
<a name="line156"></a> * browser dependent properties of the iframe.
<a name="line157"></a> * @return {boolean} whether or not the iframe is loaded.
<a name="line158"></a> * @private
<a name="line159"></a> */
<a name="line160"></a>goog.net.IframeLoadMonitor.prototype.isLoadedHelper_ = function() {
<a name="line161"></a>  var isLoaded = false;
<a name="line162"></a>  /** @preserveTry */
<a name="line163"></a>  try {
<a name="line164"></a>    // IE will reliably have readyState set to complete if the iframe is loaded
<a name="line165"></a>    // For everything else, the iframe is loaded if there is a body and if the
<a name="line166"></a>    // body should have content the firstChild exists.  Firefox can fire
<a name="line167"></a>    // the LOAD event and then a few hundred ms later replace the
<a name="line168"></a>    // contentDocument once the content is loaded.
<a name="line169"></a>    isLoaded = goog.userAgent.IE ? this.iframe_.readyState == &#39;complete&#39; :
<a name="line170"></a>        !!goog.dom.getFrameContentDocument(this.iframe_).body &amp;&amp;
<a name="line171"></a>        (!this.hasContent_ ||
<a name="line172"></a>        !!goog.dom.getFrameContentDocument(this.iframe_).body.firstChild);
<a name="line173"></a>  } catch (e) {
<a name="line174"></a>    // Ignore these errors. This just means that the iframe is not loaded
<a name="line175"></a>    // IE will throw error reading readyState if the iframe is not appended
<a name="line176"></a>    // to the dom yet.
<a name="line177"></a>    // Firefox will throw error getting the iframe body if the iframe is not
<a name="line178"></a>    // fully loaded.
<a name="line179"></a>  }
<a name="line180"></a>  return isLoaded;
<a name="line181"></a>};
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/**
<a name="line185"></a> * Handles an event indicating that the loading status of the iframe has
<a name="line186"></a> * changed.  In Firefox this is a goog.events.EventType.LOAD event, in IE
<a name="line187"></a> * this is a goog.events.EventType.READYSTATECHANGED
<a name="line188"></a> * @private
<a name="line189"></a> */
<a name="line190"></a>goog.net.IframeLoadMonitor.prototype.handleLoad_ = function() {
<a name="line191"></a>  // Only do the handler if the iframe is loaded.
<a name="line192"></a>  if (this.isLoadedHelper_()) {
<a name="line193"></a>    this.maybeStopTimer_();
<a name="line194"></a>    goog.events.unlistenByKey(this.onloadListenerKey_);
<a name="line195"></a>    this.onloadListenerKey_ = null;
<a name="line196"></a>    this.isLoaded_ = true;
<a name="line197"></a>    this.dispatchEvent(goog.net.IframeLoadMonitor.LOAD_EVENT);
<a name="line198"></a>  }
<a name="line199"></a>};
</pre>


</body>
</html>
